package com.ruoyi.washer.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.washer.common.*;
import com.ruoyi.washer.service.impl.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/wechat")
public class WeChatController02 {
    private static final Logger logger = LoggerFactory.getLogger(WeChatController02.class);

    @Autowired
    private WxWechatTools wxWechatTools;

    @Autowired
    private XwAgentServiceImpl xwAgentServiceImpl;

    @Autowired
    private XwDeviceServiceImpl xwDeviceServiceImpl;

    @Autowired
    private XwWashballServiceImpl xwWashballServiceImpl;

    @Autowired
    private XwEnduserServiceImpl xwEnduserServiceImpl;

    @Autowired
    private XwEnduserTeamServiceImpl xwEnduserTeamServiceImpl;

    @Autowired
    private XwProductServiceImpl xwProductServiceImpl;

    @Autowired
    private WxPayTool wxPayTool;

    @Autowired
    private PaymentPayServiceImpl paymentPayServiceImpl;

    @Autowired
    private PaymentDivideServiceImpl paymentDivideServiceImpl;

    @Autowired
    private PlatformToolServiceImpl platformToolServiceImpl;

    @Autowired
    private PaymentRefundServiceImpl paymentRefundServiceImpl;

    /*
    出售发起订单
     */
    @GetMapping("/open/buy/pay")
    public AjaxResult initBuyPay(
                            @RequestParam("deviceSn") String deviceSn,
                            @RequestParam("index") int index,
                            @RequestParam("productId") String productId,
                            @RequestParam("openId") String openId){
        AjaxResult ajax = AjaxResult.success();
        try{
            ajax.put("data",paymentPayServiceImpl.initBuyPay(deviceSn,index,productId,openId));
        }catch (Exception e){
            ajax=new AjaxResult(HttpStatus.BAD_REQUEST, "发起支付异常");
        }
        return ajax;
    }

    @GetMapping("/open/payscore/queryOrder")
    public AjaxResult queryOrder(@RequestParam(required = false) String outOrderNo, @RequestParam(required = false) String queryId) {
        return paymentPayServiceImpl.queryOrder(outOrderNo,queryId);
    }

    /*
    出租发起订单
     */
    @GetMapping("/open/rent/pay")
    public AjaxResult initRentPay(
            @RequestParam("deviceSn") String deviceSn,
            @RequestParam("washballSn") String washballSn,
            @RequestParam("openId") String openId,
            @RequestParam("perhourPrice") String perhourPrice, @RequestParam(value = "payType", required = false) String payType) {
        //2024年7月24日 12:30:33  增加payType 支持微信支付分出租 不传就是默认值
        AjaxResult ajax = null;
        try{
            JSONObject jsonObject=paymentPayServiceImpl.initRentPay(deviceSn,washballSn,openId,perhourPrice, payType);
            if(jsonObject.getBoolean("flag")){
                ajax=new AjaxResult(HttpStatus.BAD_REQUEST, "有相同的订单还在使用中，需要结束上一个订单");
            }else {
                ajax=new AjaxResult(HttpStatus.SUCCESS, "处理成功");
            }
            ajax.put("data",jsonObject);
        }catch (Exception e){
            logger.error("租用发起异常信息：",e.getMessage());
            ajax=new AjaxResult(HttpStatus.BAD_REQUEST, "租用发起支付异常");
        }
        return ajax;
    }
    /*
      支付回调通知
     */
    @PostMapping(value = "/open/pay/notify")
    public AjaxResult wechatPayNotify(HttpServletRequest request){
        AjaxResult ajax = AjaxResult.success();
        try{
            paymentPayServiceImpl.payNotifyAll(request);
            logger.info("-----------------------A支付回调成功----------------------------");
        }catch (Exception e){
            logger.info("-----------------------B支付回调失败----------------------------");
            logger.info(e.getMessage());

        }
        return ajax;
    }
    /*
    保存设备购买后的状态
     */
    @GetMapping("/open/save/buy/state")
    public AjaxResult saveDeviceBallBuyState(
            @RequestParam("deviceSn") String deviceSn,
            @RequestParam("index") int index){
        AjaxResult ajax = AjaxResult.success();
        try{
            int flag=xwDeviceServiceImpl.saveDeviceBallBuyState(deviceSn,index);
            if(flag!=1){
                ajax=new AjaxResult(HttpStatus.BAD_REQUEST, "更新设备状态异常");
            }
        }catch (Exception e){
            ajax=new AjaxResult(HttpStatus.BAD_REQUEST, "更新设备状态异常");
        }
        return ajax;
    }
    /*
    发起分账
     */
    @GetMapping("/open/divide")
    public AjaxResult divide(){
        AjaxResult ajax = AjaxResult.success();
        //ajax.put("data",paymentDivideServiceImpl.createDivideOrder(null,null,null));
        return ajax;
    }
    /*
    查询分账
    */
    @GetMapping("/open/query/divide")
    public AjaxResult queryDivide()throws Exception{
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data",paymentDivideServiceImpl.queryDivideOrder(null));
        return ajax;
    }

    /*
    增加分账人
    */
    @GetMapping("/open/add/divide/user")
    public AjaxResult addDividUser()throws Exception{
        AjaxResult ajax = AjaxResult.success();
        //ajax.put("data",paymentDivideServiceImpl.addDividUser());
        return ajax;
    }

    /*
删除分账人
*/
    @GetMapping("/open/delete/divide/user")
    public AjaxResult deleteDividUser()throws Exception{
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data",paymentDivideServiceImpl.deleteDividUser());
        return ajax;
    }
    /*
获取平台证书清单
*/
    @GetMapping("/open/get/platform/key")
    public AjaxResult getPlatformKey()throws Exception{
        AjaxResult ajax = AjaxResult.success();
        ajax.put("data",platformToolServiceImpl.getPlatformKey());
        return ajax;
    }
    /*
退款回调
 */
    @PostMapping(value = "/open/refund/notify")
    public AjaxResult RefundNotify(HttpServletRequest request){
        AjaxResult ajax = AjaxResult.success();
        try{
            paymentRefundServiceImpl.refundNotifyAll(request);
            logger.info("-----------------------A-back退款回调成功----------------------------");
        }catch (Exception e){
            logger.info("-----------------------B-back退款回调失败----------------------------");
            logger.info(e.getMessage());

        }
        return ajax;
    }
}
